package com.ruicar.afs.cloud.apply.business.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.apply.business.mapper.ApplySignRelationMapper;
import com.ruicar.afs.cloud.apply.business.service.ApplySignRelationService;
import com.ruicar.afs.cloud.apply.common.entity.ApplyCarInvoice;
import com.ruicar.afs.cloud.apply.common.entity.ApplySignRelation;
import com.ruicar.afs.cloud.apply.contract.condition.ComAttachmentCondition;
import com.ruicar.afs.cloud.apply.contract.service.ApplyCarInvoiceService;
import com.ruicar.afs.cloud.apply.contract.vo.SignRelationVO;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.parameter.commom.enums.WhetherEnum;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: mingzhi.li
 * @date: 2020/8/7 16:45
 * @description:合同签约关系
 */
@Service
@AllArgsConstructor
public class ApplySignRelationServiceImpl extends ServiceImpl<ApplySignRelationMapper, ApplySignRelation> implements ApplySignRelationService {

    private ApplyCarInvoiceService applyCarInvoiceService;

    /**
     * 获取签约关系信息-主共保
     * @param condition
     * @return
     */
    public List<SignRelationVO> getSignRelation(ComAttachmentCondition condition){

        return this.baseMapper.getSignRelation(condition);
    }

    /**
     * 获取签约关系信息-见证人
     * @param condition
     * @return
     */
    public List<SignRelationVO> getWitnessRelation(ComAttachmentCondition condition){

        return this.baseMapper.getWitnessRelation(condition);
    }

    /**
     * 通过业务编号、模板ID、客户ID查询签约关系信息
     * @param businessNo
     * @param templeId
     * @param custId
     * @return
     */
    public ApplySignRelation getSignRelationForParam(String businessNo,Long templeId,Long custId){

        ApplySignRelation applySignRelation = this.baseMapper.selectOne(Wrappers.<ApplySignRelation>query().lambda()
                .eq(ApplySignRelation::getBusinessNo,businessNo).eq(ApplySignRelation::getTempleId,templeId).eq(ApplySignRelation::getCustId,custId));
        return applySignRelation;
    }

    /**
     * 保存签约关系信息-进件和放款
     * @param condition
     */
    public void saveSignRelation(ComAttachmentCondition condition){

        // 主共保
        List<SignRelationVO> list = this.getSignRelation(condition);
        if(list!=null && list.size()>0){
            for(SignRelationVO vo:list){
                // 判断签约关系信息是否存在
                ApplySignRelation signRelation = this.getSignRelationForParam(vo.getBusiNo(),vo.getId(),vo.getCustId());
                if(signRelation != null){
                    continue;
                }
                signRelation = new ApplySignRelation();
                signRelation.setBusinessNo(vo.getBusiNo());
                signRelation.setTempleId(vo.getId());
                signRelation.setTempleName(vo.getPrintFormName());
                signRelation.setCustId(vo.getCustId());
                signRelation.setCustName(vo.getCustName());
                signRelation.setStatus(WhetherEnum.No.getIndex());// 0-未签约
                signRelation.setCustRole(vo.getCustRole());
                this.save(signRelation);
            }
        }
        // add by sijun.yu 2020-9-15
        List<SignRelationVO> list2 = this.getWitnessRelation(condition);
        if(list2!=null && list2.size()>0){
            for(SignRelationVO vo:list2){
                // 04-见证人
                if("04".equals(vo.getCustRole())){
                    Long userId = SecurityUtils.getUser().getId();
                    String userRealName = SecurityUtils.getUser().getUserRealName();
                    ApplySignRelation signRelation = this.getSignRelationForParam(vo.getBusiNo(),vo.getId(),userId);
                    if(signRelation != null){
                        continue;
                    }
                    signRelation = new ApplySignRelation();
                    signRelation.setBusinessNo(vo.getBusiNo());
                    signRelation.setTempleId(vo.getId());
                    signRelation.setTempleName(vo.getPrintFormName());
                    signRelation.setCustId(userId);
                    signRelation.setCustName(userRealName);
                    signRelation.setStatus(WhetherEnum.No.getIndex());// 0-未签约
                    signRelation.setCustRole(vo.getCustRole());
                    this.save(signRelation);
                }
                // 05-车商
                else if("05".equals(vo.getCustRole())){
                    ApplyCarInvoice carInvoice = applyCarInvoiceService.getApplyCarInvoice(condition.getApplyNo());
                    if(carInvoice!=null){
                        ApplySignRelation signRelation = this.getSignRelationForParam(vo.getBusiNo(),vo.getId(),carInvoice.getId());
                        if(signRelation != null){
                            continue;
                        }
                        if("personal".equals(carInvoice.getInvoiceNature())){
                            signRelation = new ApplySignRelation();
                            signRelation.setBusinessNo(vo.getBusiNo());
                            signRelation.setTempleId(vo.getId());
                            signRelation.setTempleName(vo.getPrintFormName());
                            signRelation.setCustId(carInvoice.getId());
                            signRelation.setCustName(carInvoice.getSaleName());
                            signRelation.setStatus(WhetherEnum.No.getIndex());// 0-未签约
                            signRelation.setCustRole(vo.getCustRole());
                            this.save(signRelation);
                        }
                    }
                }
            }
        }
    }

    /**
     * 保存电子签约关系-进件和放款
     * @param comAttachmentFileList
     * @return
     */
    public void saveSignRelationList(List<ComAttachmentFile> comAttachmentFileList,String applyNo) {

        if (comAttachmentFileList!=null && comAttachmentFileList.size() > 0) {
            for (ComAttachmentFile comAttachmentFile : comAttachmentFileList) {
                ComAttachmentCondition condition = new ComAttachmentCondition();
                BeanUtils.copyProperties(comAttachmentFile,condition);
                condition.setApplyNo(applyNo);
                this.saveSignRelation(condition);
            }
        }
    }

    /**
     * 获取签约关系表数据-预审批
     * @param condition
     * @return
     */
    public List<SignRelationVO> getPreRelation(ComAttachmentCondition condition){

        return this.baseMapper.getPreRelation(condition);
    }

    /**
     * 保存签约关系信息-预审批
     * @param condition
     */
    public void savePreRelation(ComAttachmentCondition condition){

        List<SignRelationVO> list = this.getPreRelation(condition);
        if(list!=null && list.size()>0){
            for(SignRelationVO vo:list){
                // 判断签约关系信息是否存在
                ApplySignRelation signRelation = this.getSignRelationForParam(vo.getBusiNo(),vo.getId(),vo.getCustId());
                if(signRelation != null){
                    continue;
                }
                signRelation = new ApplySignRelation();
                signRelation.setBusinessNo(vo.getBusiNo());
                signRelation.setTempleId(vo.getId());
                signRelation.setTempleName(vo.getPrintFormName());
                signRelation.setCustId(vo.getCustId());
                signRelation.setCustName(vo.getCustName());
                signRelation.setStatus(WhetherEnum.No.getIndex());// 0-未签约
                signRelation.setCustRole(vo.getCustRole());
                this.save(signRelation);
            }
        }
    }

    /**
     * 保存电子签约关系-进件和放款
     * @param comAttachmentFileList
     * @return
     */
    public void savePreRelationList(List<ComAttachmentFile> comAttachmentFileList) {

        if (comAttachmentFileList!=null && comAttachmentFileList.size() > 0) {
            for (ComAttachmentFile comAttachmentFile : comAttachmentFileList) {
                ComAttachmentCondition condition = new ComAttachmentCondition();
                BeanUtils.copyProperties(comAttachmentFile,condition);
                this.savePreRelation(condition);
            }
        }
    }

}
